using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GoDive.Contract;
using GoDive.Service;

namespace GoDive.App
{
    public class HomeController : Controller
    {

        GoDiveDBContext db = new GoDiveDBContext();

        public ActionResult Default()
        {
            var banner = db.tbanner.Where(x => x.Type == 1).Select(x => new tbanner
            {
                ID = x.ID,
                Title = x.Title,
                Url = x.Url,
                OrderIndex = x.OrderIndex,
                LargeImg = x.LargeImg,
                Desc = x.Desc
            }).ToList();

            return View("index");
        }

        public ActionResult Index()
        {
            HomeViewModel homeModel = new HomeViewModel();
            var banners = db.tbanner.Where(x => x.Type == 1).Select(x => new
            {
                ID = x.ID,
                Title = x.Title,
                Url = x.Url,
                OrderIndex = x.OrderIndex,
                LargeImg = x.LargeImg,
                SmallImg = x.SmallImg,
                Desc = x.Desc
            }).AsEnumerable().Select(x => new tbanner
            {
                ID = x.ID,
                Title = x.Title,
                Url = x.Url,
                OrderIndex = x.OrderIndex,
                LargeImg = x.LargeImg,
                SmallImg = x.SmallImg,
                Desc = x.Desc
            }).ToList();

            homeModel.Banners = banners;

            var destinations = db.tdestination.Where(x => x.IsHome)
                .Select(x => new
                {
                    ID = x.ID,
                    Pic = x.Pic,
                    Name = x.Name,
                    Summary = x.Summary,
                    OrderIndex = x.OrderIndex
                })
            .OrderByDescending(x => x.OrderIndex).AsEnumerable()
            .Select(x => new tdestination
            {
                ID = x.ID,
                Pic = x.Pic,
                Name = x.Name,
                Summary = x.Summary,
                OrderIndex = x.OrderIndex
            }).ToList();

            homeModel.Destinations = destinations;

            int pagesize = 10;
            int articleCount = db.tarticle.Count();
            if (articleCount > pagesize)
            {
                homeModel.ArticleHasNextPage = true;
            }
            homeModel.ArticlePageIndex = 1;

            ArticleController articleController = new ArticleController();
            var articles = articleController.GetPagedList(1, 5, 0);
            homeModel.Articles = articles.Rows;

            return View(homeModel);
        }
    }
}
